using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Globalization;

namespace CSVReader
{
    class CSVReader
    {
        public void Read(string path)
        {
            DataTable dt = new DataTable();
            List<string> slist = readLines(path);
            createCSVTable(slist);
        }

        private List<string> readLines(string path)
        {
            List<string> slist = new List<string>();
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
                {
                    while (!sr.EndOfStream)
                    {
                        string s = sr.ReadLine();
                        slist.Add(s);
                    }
                }
            }
            return slist;
        }

        private void createCSVTable(List<string> slist)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("id",System.Type.GetType("System.Int32")));
            dt.Columns.Add(new DataColumn("ss", System.Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("sss", System.Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("f", System.Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("d", System.Type.GetType("System.String")));

            foreach (string s in slist)
            {
                List<string> ss = splitItem(s);

                Console.WriteLine("================");
                foreach (string str in ss)
                {
                    Console.WriteLine(str);
                }

                Console.WriteLine("================");
                //DataRow dr = dt.NewRow();
                //string[] ss = splitItem(s);
                //dr["id"] = int.Parse(ss[0]);
                //dr["ss"] = ss[1];
                //dr["sss"] = ss[2];
                //dr["f"] = ss[3];
                //dr["d"] = ss[4];
                //dt.Rows.Add(dr);
            }
        }

        private List<string> splitItem(string s)
        {
            s=s.Replace("\"", "\r\n");
            while (s.IndexOf("\r\n\r\n") > 0)
            {
                s = s.Replace("\r\n\r\n", "\"");
            }
            List<string> results = new List<string>();
            while (s.Length >0)
            {
                string stemp = getString(ref s);
                results.Add(stemp);
            }

            return results;
        }

        private string getString(ref string s)
        {
            string result = "";
            if (s.StartsWith("\r\n"))
            {
                int end = s.IndexOf("\r\n", 2);
                result = s.Substring(2, end - 2);
                if (s.Length > end + 3) s = s.Substring(end + 3);
                else s = "";
            }
            else
            {
                result = s.Split(',')[0];
                s = s.Replace(result, "").TrimStart(',');
            }
            return result;
        }
    }
}
